#include "usr_inc.h"

extern Dji_t dji3;
extern Dji_t dji4;

/**
 * @brief 初始化所有CAN控制器
 */
void ALL_BSP_CAN_Init(void)
{
    CAN_FilterTypeDef canFliter = {0};

    canFliter.FilterBank = 0;
    canFliter.FilterMode = CAN_FILTERMODE_IDMASK;
    canFliter.FilterScale = CAN_FILTERSCALE_32BIT;
    canFliter.FilterIdHigh = 0x0000;
    canFliter.FilterIdLow = 0x0000;
    canFliter.FilterMaskIdHigh = 0x0000;
    canFliter.FilterMaskIdLow = 0x0000;
    canFliter.FilterFIFOAssignment = CAN_RX_FIFO0;
    canFliter.FilterActivation = ENABLE;
    canFliter.SlaveStartFilterBank = 14;
    HAL_CAN_ConfigFilter(&hcan1, &canFliter);

    canFliter.FilterBank = 14;
    canFliter.FilterMode = CAN_FILTERMODE_IDMASK;
    canFliter.FilterScale = CAN_FILTERSCALE_32BIT;
    canFliter.FilterIdHigh = 0x0000;
    canFliter.FilterIdLow = 0x0000;
    canFliter.FilterMaskIdHigh = 0x0000;
    canFliter.FilterMaskIdLow = 0x0000;
    canFliter.FilterFIFOAssignment = CAN_RX_FIFO0;
    canFliter.FilterActivation = ENABLE;
    canFliter.SlaveStartFilterBank = 14;
    HAL_CAN_ConfigFilter(&hcan2, &canFliter);

    __HAL_CAN_ENABLE_IT(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING);
    __HAL_CAN_ENABLE_IT(&hcan2, CAN_IT_RX_FIFO0_MSG_PENDING);

    HAL_CAN_Start(&hcan1);
    HAL_CAN_Start(&hcan2);
}

/**
 * @brief 重定义RxFifo0的中断回调函数
 */
void HAL_CAN_RxFifo0MsgPendingCallback(CAN_HandleTypeDef *hcan)
{
    if (hcan->Instance == CAN1)
    {
        CAN_RxHeaderTypeDef rxHeader;
        uint8_t rxData[8];
        HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &rxHeader, rxData);

        if (rxHeader.StdId == 0x201)
        {
            TossCmdMsg_t cmd = TOSS_DRAG;
            xQueueSendToBackFromISR(tossCmdQueueHandle, &cmd, NULL);
        }
        else if (rxHeader.StdId == 0x203)
        {
            TossCmdMsg_t cmd = TOSS_FREE;
            xQueueSendToBackFromISR(tossCmdQueueHandle, &cmd, NULL);
        }
        else if (rxHeader.StdId == 0x204)
        {
            TossCmdMsg_t cmd = TOSS_ABANDON;
            xQueueSendToBackFromISR(tossCmdQueueHandle, &cmd, NULL);
        }
        else if (rxHeader.StdId == 0x211)
        {
            TossCmdMsg_t cmd = TOSS_UP;
            xQueueSendToBackFromISR(tossCmdQueueHandle, &cmd, NULL);
        }
        else if (rxHeader.StdId == 0x212)
        {
            TossCmdMsg_t cmd = TOSS_DOWN;
            xQueueSendToBackFromISR(tossCmdQueueHandle, &cmd, NULL);
        }
        else if (rxHeader.StdId == 0x220)
        {
            float stroke;
            memcpy(&stroke, rxData + 0, sizeof(float));
            xQueueSendToBackFromISR(tossStrokeQueueHandle, &stroke, NULL);
        }
        else if (rxHeader.StdId == 0x301)
        {
            DribbleCmdMsg_t cmd = DRIBBLE_EXTEND;
            xQueueSendToBackFromISR(dribbleCmdQueueHandle, &cmd, NULL);
        }
        else if (rxHeader.StdId == 0x302)
        {
            DribbleCmdMsg_t cmd = DRIBBLE_RETRACT;
            xQueueSendToBackFromISR(dribbleCmdQueueHandle, &cmd, NULL);
        }
        else if (rxHeader.StdId == 0x303)
        {
            DribbleCmdMsg_t cmd = DRIBBLE_DRAW;
            xQueueSendToBackFromISR(dribbleCmdQueueHandle, &cmd, NULL);
        }
        else if (rxHeader.StdId == 0x304)
        {
            DribbleCmdMsg_t cmd = DRIBBLE_FREE;
            xQueueSendToBackFromISR(dribbleCmdQueueHandle, &cmd, NULL);
        }
        else if (rxHeader.StdId == 0x305)
        {
            DribbleCmdMsg_t cmd = DRIBBLE_PAT;
            xQueueSendToBackFromISR(dribbleCmdQueueHandle, &cmd, NULL);
        }
        else if (rxHeader.StdId == 0x306)
        {
            DribbleCmdMsg_t cmd = DRIBBLE_HOLD;
            xQueueSendToBackFromISR(dribbleCmdQueueHandle, &cmd, NULL);
        }
        else if (rxHeader.StdId == 0x307)
        {
            DribbleCmdMsg_t cmd = DRIBBLE_RETRY;
            xQueueSendToBackFromISR(dribbleCmdQueueHandle, &cmd, NULL);
        }
    }
    else if (hcan->Instance == CAN2)
    {
        CAN_RxHeaderTypeDef rxHeader;
        uint8_t rxData[8];
        HAL_CAN_GetRxMessage(hcan, CAN_RX_FIFO0, &rxHeader, rxData);

        if (rxHeader.StdId == dji3.motorID + 0x200)
        {
            ISR_DJI_UpdateInfo(rxData, &dji3, dji3InfoBoxHandle);
        }
        else if (rxHeader.StdId == dji4.motorID + 0x200)
        {
            ISR_DJI_UpdateInfo(rxData, &dji4, dji4InfoBoxHandle);
        }
    }
}
